﻿
using Kylin.Wiki.IService;
using Kylin.Wiki.Model.Api;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Model.Entity;
using Kylin.Wiki.Model.View;
using Kylin.Wiki.Web.Common;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace Kylin.Wiki.Web.Controllers.System
{

    [Route("api/[controller]")]
    [ApiController]
    public class MenuController : BaseController
    {
        private readonly IMenuService _menuService;

        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }
        /// <summary>
        /// 根据当前用户获取菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet, Description("根据当前用户获取菜单")]
        public ApiResult<List<MenuVM>> Get()
        {
            return _menuService.GetMenus(UserContext.Current.UserId);
        }

        [HttpGet("GetList")]
        public ApiResult<List<MenuVM>> GetList()
        {
            return _menuService.GetList();
        }

        [HttpGet("GetListByRID")]
        public ApiResult<List<MenuVM>> GetListByRID(int roleId)
        {
            return _menuService.GetListByRoleID(roleId);
        }

        [HttpGet("GetListByPage")]
        public ApiPageResult<List<MenuVM>> GetListByPage(MenuDTO menuDTO)
        {
            return _menuService.GetListByPage(menuDTO);
        }

        [HttpPost]
        public ApiResult<bool> Create([FromBody]Menu menu)
        {
            menu.CreateBy = UserContext.Current.UserName;
            menu.CreateOn = DateTime.Now;
            menu.UpdateBy = UserContext.Current.UserName;
            menu.UpdateOn = DateTime.Now;
            menu.IsDelete = "0";
            menu.Status = false;
            return _menuService.Create(menu);
        }

        [HttpPut]
        public ApiResult<bool> Update([FromBody] Menu menu)
        {
            menu.UpdateBy = UserContext.Current.UserName;
            menu.UpdateOn = DateTime.Now;
            return _menuService.Update(menu);
        }

        [HttpDelete("{id}")]
        public ApiResult<bool> Delete(int id)
        {
            var menu = _menuService.FindFirst(x => x.MenuID == id);
            return _menuService.Delete(menu);
        }
    }
}
